from django.db import models

# Create your models here.

class Process(models.Model):
    '''工序表'''
    name = models.CharField(null=False,max_length=32,verbose_name="工序名称")   # 创建一个varchar(32)类型的字段，不为空

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "工序表" # 单数形式显示的字段
        verbose_name_plural = verbose_name  # 复数形式显示字段，默认admin后台显示复数形式


class SubProcess(models.Model):
    '''子工序表，一个工序中包含多个子工序'''
    name = models.CharField(null=False,max_length=32,verbose_name="子工序名称")
    process = models.ForeignKey(to="Process",on_delete=models.PROTECT,verbose_name="所属工序")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "子工序表" # 单数形式显示的字段
        verbose_name_plural = verbose_name  # 复数形式显示字段，默认admin后台显示复数形式


class DeviceType(models.Model):
    '''设备类型表'''
    typename = models.CharField(max_length=64,verbose_name="设备类型")

    def __str__(self):
        return self.typename

    class Meta:
        verbose_name = "设备类型表" # 单数形式显示的字段
        verbose_name_plural = verbose_name  # 复数形式显示字段，默认admin后台显示复数形式


class Device(models.Model):
    '''设备表'''
    name = models.CharField(max_length=64,verbose_name="设备名称")
    sn = models.CharField(max_length=32,null=False,verbose_name="设备编号")
    devicetype = models.ForeignKey(to="DeviceType",on_delete=models.PROTECT,verbose_name="设备类型")
    process = models.ForeignKey(to="Process",on_delete=models.PROTECT,verbose_name="工序")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "设备表" # 单数形式显示的字段
        verbose_name_plural = verbose_name  # 复数形式显示字段，默认admin后台显示复数形式


class DeviceWorkingHours(models.Model):
    '''机器机时表'''
    device = models.ForeignKey(to="Device",on_delete=models.PROTECT,verbose_name="设备编号")
    hours = models.FloatField(null=False,blank=False,verbose_name="机器机时")
    workdate = models.DateField(null=False,auto_now_add=True,blank=False,verbose_name="工作日期")

    class Meta:
        verbose_name = "机器机时表"
        verbose_name_plural = verbose_name


class Employees(models.Model):
    '''人员表'''
    PROPERTIES = (
        (1,'正式工'),
        (2,'临时工'),
        (3,'派遣工'),
        (4,'部门援助'),
    )
    STATUS = (
        (1,'在职'),
        (2,'离职'),
    )
    jobnumber = models.CharField(max_length=10,null=False,unique=True,verbose_name="员工工号")
    jobname = models.CharField(max_length=10,null=False,blank=False,verbose_name="员工姓名")
    subprocess = models.ForeignKey(to="SubProcess",on_delete=models.PROTECT,verbose_name="默认工序",related_name='employee')
    properties = models.IntegerField(verbose_name="性质",null=False,blank=False,choices=PROPERTIES,default=1,help_text="员工的性质，1：正式工，2：临时工，3：派遣工，4：部门援助")
    status = models.IntegerField(verbose_name="状态",null=False,blank=False,choices=STATUS,default=1,help_text="员工的状态，1：在职，2：离职，0：除正式工外状态都为 0")

    def __str__(self):
        return self.jobname

    class Meta:
        verbose_name = "人员表" # 单数形式显示的字段
        verbose_name_plural = verbose_name  # 复数形式显示字段，默认admin后台显示复数形式


class StaffHours(models.Model):
    '''人员工时表'''
    Employee = models.ForeignKey(to="Employees",on_delete=models.PROTECT,verbose_name="员工姓名",related_name='staffhours')
    process = models.CharField(max_length=100,verbose_name="工作工序",blank=False)
    hours = models.FloatField(null=False,verbose_name="人员工时",blank=False)
    workdate = models.DateField(null=False,verbose_name="工作日期",auto_now_add=True,blank=False)

    class Meta:
        verbose_name = "人员工时表"
        verbose_name_plural = verbose_name